package com.imedway.ihe.dtz.xds.registry;

import java.util.ArrayList;
import java.util.List;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLExtrinsicObject;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryError;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryError30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryResponse30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLSubmitObjectsRequest30;
import org.openehealth.ipf.commons.ihe.xds.core.responses.ErrorCode;
import org.openehealth.ipf.commons.ihe.xds.core.responses.Severity;
import org.openehealth.ipf.commons.ihe.xds.core.responses.Status;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.lcm.SubmitObjectsRequest;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryError;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryErrorList;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryResponseType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.imedway.ihe.dtz.xds.cons.SysConsts;
import com.imedway.ihe.dtz.xds.registry.sub.Iti61SubRegistry;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
public class Iti61Registry {
	
	@Autowired
	private Iti61SubRegistry iIti61SubRegistry;
	
	public RegistryResponseType run(SubmitObjectsRequest body) {

		RegistryResponseType registryResponseType = new RegistryResponseType();
		EbXMLSubmitObjectsRequest30 ebXMLbody= new EbXMLSubmitObjectsRequest30(body);
		registryResponseType.setRegistryErrorList(new RegistryErrorList());
		List<EbXMLRegistryError> registryError = new ArrayList<EbXMLRegistryError>();
		EbXMLRegistryResponse30 ebXMLRegistryResponse30 = new EbXMLRegistryResponse30(registryResponseType);
		try {		
			
			List<EbXMLExtrinsicObject> ebXMLExtrinsicObjectList= ebXMLbody.getExtrinsicObjects(SysConsts.DocumentEntryObjectTypeForStableDocumentEntries);				
			if(!ebXMLExtrinsicObjectList.isEmpty()) {
				genError(registryError, Severity.ERROR, ErrorCode.MISSING_DOCUMENT_METADATA.getOpcode(),
						"存在非On-Demand Document Entries");	
				ebXMLRegistryResponse30.setErrors(registryError);
			}else {
				iIti61SubRegistry.run(ebXMLRegistryResponse30,ebXMLbody);
			}
			
		} catch (Exception e) {
			log.info(e.getMessage(), e);
			genError(registryError, Severity.ERROR, ErrorCode.MISSING_DOCUMENT_METADATA.getOpcode(),
					e.getMessage());	
			ebXMLRegistryResponse30.setErrors(registryError);
		}
		
		ebXMLRegistryResponse30.setErrors(registryError);
		ebXMLRegistryResponse30.setStatus(!registryError.isEmpty() ? Status.SUCCESS : Status.FAILURE);
		
		return ebXMLRegistryResponse30.getInternal();
	}
	private void genError(List<EbXMLRegistryError> registryError,Severity severity,String errorCode,String codeContext) {
		EbXMLRegistryError30 re = new EbXMLRegistryError30(new RegistryError());
		re.setSeverity(severity);
		re.setErrorCode(errorCode);
		re.setCodeContext(codeContext);
		registryError.add(re);
	}

}
